package cn.fuzongyao.learning.android.geoquiz;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class CheatActivity extends AppCompatActivity {

    private static final String TAG = "QuizActivity";
    private static final String EXTRA_ANSWER_IS_TRUE = "answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "answer_shown";
    private static final String CHEATER_KEY = "is_cheater";
    private boolean isCheater;
    private TextView answerTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        boolean answerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
        answerTextView = findViewById(R.id.answer_text_view);

        if (savedInstanceState != null) {
            isCheater = savedInstanceState.getBoolean(CHEATER_KEY, false);
            setAnswerShownResult(isCheater);
            setAnswerTextView(answerIsTrue);
        }

        Button showAnswerButton = findViewById(R.id.show_answer_button);
        showAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "sdk version: " + Build.VERSION.SDK_INT);
                isCheater = true;
                setAnswerTextView(answerIsTrue);

                int cx = showAnswerButton.getWidth() / 2;
                int cy = showAnswerButton.getHeight() / 2;
                float radius = showAnswerButton.getWidth();
                Animator anim = ViewAnimationUtils
                        .createCircularReveal(showAnswerButton, cx, cy, radius, 0);
                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        showAnswerButton.setVisibility(View.INVISIBLE);
                    }
                });
                anim.start();

                setAnswerShownResult(true);
            }
        });
    }

    private void setAnswerTextView(boolean answerIsTrue) {
        if (answerIsTrue) {
            answerTextView.setText(R.string.true_button);
        } else {
            answerTextView.setText(R.string.false_button);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CHEATER_KEY, isCheater);
    }

    public static Intent newIntent(Context context, boolean answerIsTrue) {
        return new Intent(context, CheatActivity.class)
                .putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    }

    public static boolean wasAnswerShown(Intent result) {
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
    }

    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent data = new Intent().putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }
}
